import request from '../utils/request'
import type {
  LoginParams,
  UserListParams,
  CreateUserParams,
  UpdateUserParams,
  AssignRoleParams
} from '../types/user'

export function login(data: LoginParams) {
  return request({
    url: '/auth/login',
    method: 'post',
    data
  })
}

export function logout() {
  return request({
    url: '/auth/logout',
    method: 'post'
  })
}

//修改用户密码
export function changePassword(data: {
  currentPassword: string
  newPassword: string
  confirmPassword: string
}) {
  return request({
    url: '/user/v1/changePassword',
    method: 'post',
    data
  })
}

export function getInfo(name: string) {
  return request({
    url: '/auth/info',
    method: 'get',
    params: { name }
  })
}

export function fetchList(params: UserListParams) {
  return request({
    url: '/user/v1/list',
    method: 'get',
    params: params
  })
}

export function createUser(data: CreateUserParams) {
  return request({
    url: '/user/v1/create',
    method: 'post',
    data: data
  })
}

export function deleteUser(id: number | null) {
  return request({
    url: `/user/v1/delete/${id}`,
    method: 'delete'
  })
}

// export function updateUser(data: UpdateUserParams) {
//   return request({
//     url: `/admin/v1/update/${data.id}`,
//     method: 'post',
//     data: data
//   })
// }

export function updateUser(data: UpdateUserParams) {
  return request({
    url: '/user/v1/update',
    method: 'put',
    data: data
  })
}

//根据ID查询用户
// export function getRolesByUser(id: string) {
//   return request({
//     url: `/user/v1/getOne/${id}`,
//     method: 'get'
//   })
// }

//根据ID查询角色
export function getRolesByUser(id: number | null) {
  return request({
    url: `/role/v1/getOne/${id}`,
    method: 'get'
  })
}

//用户绑定角色
export function assignUserRole(data: AssignRoleParams) {
  return request({
    url: '/userRole/v1/create',
    method: 'post',
    data
  })
}

/**更新用户角色关联 */
export function updateUserRole(data: AssignRoleParams) {
  return request({
    url: '/userRole/v1/update',
    method: 'put',
    data: data
  })
}

export function deleteUserRole(userId: number | null) {
  return request({
    url: `/userRole/v1/delete/${userId}`,
    method: 'delete'
  })
}

//添加用户联系人
export function addUserByContact(data) {
  return request({
    url: '/user/v1/contact/add',
    method: 'post',
    data
  })
}
//添加用户家属关联
export function addUserByFamily(data) {
  return request({
    url: '/user/v1/family/add',
    method: 'post',
    data
  })
}
//添加用户医护人员
export function addUserByMedical(data) {
  return request({
    url: '/user/v1/medical/add',
    method: 'post',
    data
  })
}

//根据用户ID查询联系人
export function getUserByContact(userId: number | null) {
  return request({
    url: `/user/v1/contact/${userId}`,
    method: 'get'
  })
}
//更新用户联系人
export function updateUserByContact(data: any) {
  return request({
    url: '/user/v1/contact/update',
    method: 'put',
    data: data
  })
}

//删除用户联系人
export function deleteUserByContact(id: number | null | undefined) {
  return request({
    url: `/user/v1/contact/remove?id=${id}`,
    method: 'delete'
  })
}

//获取用户联系人列表
export function getUserContactList(userId: number | null, params?: any) {
  return request({
    url: `/user/v1/contact/${userId}`,
    method: 'get',
    params
  })
}

//获取用户家属列表
export function getUserFamilyList(params: { userId: number | null }) {
  return request({
    url: '/user/v1/family/list',
    method: 'get',
    params
  })
}
//删除用户跟家属的关联
export function deleteUserFamily(familyId: number | null, userId: number | null) {
  return request({
    url: `/user/v1/family/remove/${familyId}/${userId}`,
    method: 'delete'
  })
}
